package vmcs.simulationsystem;

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.util.Observable;

import vmcs.store.DrinksBrand;
import vmcs.store.StoreItem;

// implements Observer interface for Observer pattern
public class DrinkSelectionItem extends Panel {

	/**
	 * Active Color
	 */     
	public final static Color ACT_COLOR = Color.white;
	/**
	 * In Stock state color
	 */     
	public final static Color IN_STK    = Color.GRAY;       
	/**
	 * Out of Stock state color
	 */     
	public final static Color OUT_STK   = Color.RED;

	private Button     drinkName;
	private TextField  drinkPrice;
	private TextField  drinkStatus;

	/**
	 * This constructor creates an instance of the Drink Selection Item. 
	 */     
	public DrinkSelectionItem(StoreItem item) {

	//	item.addObserver(this);
		DrinksBrand db = (DrinksBrand)item.getContent();
//		db.addObserver(this);

		drinkName   = new Button();
		drinkName.setFont(new Font("Helvetica", Font.BOLD, 14));
		drinkName.setBackground(ACT_COLOR);

		drinkPrice  = new TextField("     ");
		drinkPrice.setEditable(false);

		drinkStatus = new TextField("No Stock");
		drinkStatus.setEditable(false);

		this.setLayout(new GridLayout(1,3));
		Panel tp = new Panel(new GridLayout(0,1));
		tp.add(drinkName);
		Panel tp1 = new Panel();
		tp1.add(drinkPrice);
		Panel tp2 = new Panel();
		tp2.add(drinkStatus);
		this.add(tp);
		this.add(tp1);
		this.add(tp2);
	}

	/**
	 * This operation activates or deactivates the drink selection item button. 
	 * This operation also displays OUT OF STOCK messages where appropriate.
	 * @param active boolean state setting the status True / False
	 */     
	public void setItemState(boolean active) {
		drinkName.setEnabled(active);
		drinkStatus.setEnabled(!active);
	}

	/**
	 * This operation sets the status of the Drinks selection Item.
	 * @param active boolean state set as Active / Deactive
	 */     
	public void setItemActive(boolean active) {
		drinkName.setEnabled(active);
		drinkStatus.setEnabled(active);
	}

	/**
	 * This operation sets the name on the Drink Selection Item. 
	 */     
	public void setName (String name) {
		drinkName.setLabel(name);
	}

	/**
	 * This operation sets the price on the Drink Selection Item
	 * @param price Price for drinks brand
	 */     
	public void setPrice (int price) {
		String wStr = String.valueOf(price) + " c";
		drinkPrice.setText(wStr);
	}

	/**
	 * This operation sets the price on the Drink Selection Item
	 * @param price Price for drinks brand
	 */     
	public void setStock (int stock) {
		if (stock == 0){
			drinkStatus.setText("No Stock");
		} else if (stock == 1){
			drinkStatus.setText(stock + " Can");
		} else {
			drinkStatus.setText(stock + " Cans");
		}
	}
	
	/**
	 * This operation attaches a listener to the Button object.
	 * @param l ActionListener
	 */     
	public void addListener(ActionListener l) {
		drinkName.addActionListener(l);
	}

	/**
	 * This operation sets the action command to the Button object.
	 * @param cmd String parameter
	 */     
	public void setActionCommand(String cmd) {
		drinkName.setActionCommand(cmd);
	}

	/*
	public void update(Observable o, Object arg) {
		if (o instanceof StoreItem) {
			StoreItem item = (StoreItem)o;
			if (item.getQuantity()>0) {
				drinkStatus.setEnabled(false);
			} else {
				drinkStatus.setEnabled(true);
			}
		}
		if (o instanceof DrinksBrand) {
			DrinksBrand db = (DrinksBrand)o;
			setPrice(db.getPrice());
		}
	}     
	*/  
}

